package com.nkzy.mapper;

import com.nkzy.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO user(username, password, phone, info, status, balance, create_time, update_time) " +
            "VALUES(#{username}, #{passwo" +
            "rd}, #{phone}, #{info}, #{status}, #{balance}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteUserById(@Param("id") Long id);  // 添加 @Param

    @Update("UPDATE user SET username=#{username}, password=#{password}, phone=#{phone}, info=#{info}, " +
            "status=#{status}, balance=#{balance}, update_time=#{updateTime} WHERE id=#{id}")
    int updateUser(User user);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectUserById(@Param("id") Long id);  // 添加 @Param

    @Select("SELECT * FROM user")
    List<User> selectAllUsers();

    @Select("<script>" +
            "SELECT * FROM user " +
            "<where>" +
            "   <if test='ids != null and !ids.isEmpty()'>" +
            "       id IN " +
            "       <foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            "           #{id}" +
            "       </foreach>" +
            "   </if>" +
            "   <if test='ids == null or ids.isEmpty()'>" +
            "       1=0" +  // 空列表时查询无结果
            "   </if>" +
            "</where>" +
            "</script>")
    List<User> selectUsersByIds(@Param("ids") List<Long> ids);
}
